#!/bin/sh
###############################################################################
##  Author    : 陈贰浩
##  Name      : edw_main.sh
##  Functions : Initinal all variable and parameter
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  陈贰浩           1. CREATED THIS SHELL.
###############################################################################
. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh
function USAGE(){
    echo "How to use this shell script!"
    echo "${V_SHELL_HOME}/edw_main.sh 20171231 D"
}

STEP_LIST=(parm_refresh batch_start data_sync_M2H proc_HIVE data_sync_H2M kylin hbase sample batch_end)
STATUS_LIST=(1 1 1 1 1 1 1 1 1)
RUN_TIME=()
function UPDATE_STEP(){
  for step in ${STEP_LIST[@]}
  do
    printf '|\033[36m\033[1m%-15s\033[0m' "${step}"
  done
  printf '\n'
}

#更新任务状态
function UPDATE_STATUS(){
  printf '\r'
  for ((i=0;i<= ${#STATUS_LIST[@]};i++));
  do
    case ${STATUS_LIST[i]} in
      ${WAIT})
          printf '|\033[1m\033[30m%-15s\033[0m' "WAIT..."
      ;;
      ${FAIL})
          printf '|\033[1m\033[31m%-15s\033[0m' "FAIL"
      ;;
      ${DONE})
          printf '|\033[1m\033[32m%-15s\033[0m' "DONW [use ${RUN_TIME[i]}s]"
      ;;
      ${RUN})
          printf '|\033[1;5m\033[33m%-15s\033[0m' "RUN..."
      ;;
      ${PROC})
          UPDATE_PROC_SPEED
      ;;
      ${SYNC})
          UPDATE_SYNC_SPEED
      ;;
    esac
  done
}
#存储过程进度
function UPDATE_PROC_SPEED(){
    JOB_CNT=$(awk -F '=' '{if($1=="JOB_CNT") sum += $2};END {print sum}' ${PROC_SPEED_FILE})
    JOB_DONE=$(awk -F '=' '{if($1=="JOB_DONE") sum += $2};END {print sum}' ${PROC_SPEED_FILE})
    printf '|\033[1;5m\033[33m%-15s\033[0m' "run[${JOB_DONE}/${JOB_CNT}]"
}
#数据同步进度
function UPDATE_SYNC_SPEED(){
    TAB_CNT=$(awk -F '=' '{if($1=="TAB_CNT") sum += $2};END {print sum}' ${SYNC_SPEED_FILE})
    TAB_EXP=$(awk -F '=' '{if($1=="TAB_EXP") sum += $2};END {print sum}' ${SYNC_SPEED_FILE})
    printf '|\033[1;5m\033[33m%-15s\033[0m' "sync[${TAB_EXP}/${TAB_CNT}]"
}

V_DATA_DATE=$1
V_FREQUENCY=$2
if [[ -z ${1} || -z ${2} ]];then
    USAGE
    exit 1
fi

RE_CODE=$(IF_DATE ${V_DATA_DATE})
if [[ ${RE_CODE} -ne 0 ]]; then
    LOGGER "ERROR" "日期\"${V_DATA_DATE}\"格式不合法！"
    exit 2
fi

:<<!
#用trap 命令设置一个对信号的监听器
#程序运行中当监听到信号,1,2,3,9,15就会删除临时文件，并退出脚本执行
#比如说，当脚本自行运行结束、被用户 Ctrl+C 掉、被 Kill 掉、终端被关闭
#系统关机或重启的情况下，都需将临时文件删除，否则脚本以后都没机会执行
#在Linux 系统shell 下可以运行trap -l 查看到所以信号
!
trap "${V_SHELL_HOME}/edw_kill.sh; exit" 1 2 3 9 15

# 创建日志文件
if [[ ! -d "${V_SHELL_LOGS}/${V_DATA_DATE}" ]];then
    mkdir -p "${V_SHELL_LOGS}/${V_DATA_DATE}"
fi

:<<!
    开始跑批
!

BEGIN
LOGGER "INFO" "跑批账期[${V_DATA_DATE}],流程总数[7]."
UPDATE_STEP
UPDATE_STATUS

## step0 刷新参数
START_TIME=$(NOW)
STATUS_LIST[0]=${RUN}
UPDATE_STATUS
${V_SHELL_HOME}/edw_parm_refresh.sh $1 $2
RE_CODE=$? 
RUN_TIME[0]=$(DIF_TIME "${START_TIME}" "$(NOW)")
if [[ ${RE_CODE} -eq 0 ]];then
    STATUS_LIST[0]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[0]=${FAIL}
    UPDATE_STATUS
    echo "[ERROR] 错误编码[${RE_CODE}] 日志文件[${V_SHELL_LOGS}/${V_DATA_DATE}/edw_parm_refresh_${V_FREQUENCY}.log]"
    exit 3
fi

## step1 更新任务
START_TIME=$(NOW)
STATUS_LIST[1]=${RUN}
UPDATE_STATUS
${V_SHELL_HOME}/edw_batch_start.sh
RE_CODE=$?
RUN_TIME[1]=$(DIF_TIME "${START_TIME}" "$(NOW)")
if [[ ${RE_CODE} -eq 0 ]];then
    STATUS_LIST[1]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[1]=${FAIL}
    UPDATE_STATUS
    echo "[ERROR] 错误编码[${RE_CODE}] 日志文件[${V_SHELL_LOGS}/${V_DATA_DATE}/edw_batch_start_${V_FREQUENCY}.log]"
    exit 4
fi



## step2 同步维度
START_TIME=$(NOW)
STATUS_LIST[2]=${SYNC}
echo "TAB_CNT=0" > ${SYNC_SPEED_FILE}
echo "TAB_EXP=0" >> ${SYNC_SPEED_FILE}
echo "TAB_IMP=0" >> ${SYNC_SPEED_FILE}
{
  while true
  do
    if [[ -f ${SYNC_SPEED_FILE} ]];then
      UPDATE_STATUS
    else
      break
    fi
    sleep 3
  done
}&
PID=${!}
${V_SHELL_HOME}/edw_data_sync.sh M2H
RE_CODE=$?
RUN_TIME[2]=$(DIF_TIME "${START_TIME}" "$(NOW)")
kill -9 ${PID}
if [[ ${RE_CODE} -eq 0 ]];then 
    STATUS_LIST[2]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[2]=${FAIL}
    UPDATE_STATUS
    echo "[ERROR] 错误编码[${RE_CODE}] 日志文件[${V_SHELL_LOGS}/${V_DATA_DATE}/edw_data_sync_${V_FREQUENCY}.log]"
    exit 5
fi


## step3 执行任务
START_TIME=$(NOW)
STATUS_LIST[3]=${PROC}
echo "JOB_CNT=0" > ${PROC_SPEED_FILE}
echo "JOB_DONE=0" >> ${PROC_SPEED_FILE}
{
  while true
  do
    if [[ -f ${PROC_SPEED_FILE} ]];then
      UPDATE_STATUS
    else
      break
    fi
    sleep 3
  done
}&
PID=${!}
${V_SHELL_HOME}/edw_proc_launcher_parallel.sh HIVE
RE_CODE=$?
RUN_TIME[3]=$(DIF_TIME "${START_TIME}" "$(NOW)")
kill -9 ${PID}
if [[ ${RE_CODE} -eq 0 ]];then
    STATUS_LIST[3]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[3]=${FAIL}
    UPDATE_STATUS
    echo "[ERROR] 错误编码[${RE_CODE}] 日志文件[${V_SHELL_LOGS}/${V_DATA_DATE}/edw_proc_launcher_parallel_${V_FREQUENCY}.log]"
    exit 6
fi


## step4 同步数据到应用数据库
START_TIME=$(NOW)
STATUS_LIST[4]=${SYNC}
echo "TAB_CNT=0" > ${SYNC_SPEED_FILE}
echo "TAB_EXP=0" >> ${SYNC_SPEED_FILE}
echo "TAB_IMP=0" >> ${SYNC_SPEED_FILE}
{
  while true
  do
    if [[ -f ${SYNC_SPEED_FILE} ]];then
      UPDATE_STATUS
   else
     break
   fi
    sleep 3
  done
}&
PID=${!}
${V_SHELL_HOME}/edw_data_sync.sh H2M
RE_CODE=$?
RUN_TIME[4]=$(DIF_TIME "${START_TIME}" "$(NOW)")
kill -9 ${PID}
if [[ ${RE_CODE} -eq 0 ]];then
    STATUS_LIST[4]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[4]=${FAIL}
    UPDATE_STATUS
    echo "[ERROR] 错误编码[${RE_CODE}] 日志文件[${V_SHELL_LOGS}/${V_DATA_DATE}/edw_data_sync_${V_FREQUENCY}.log]"
    exit 7
fi


## step5 kylin数据build
START_TIME=$(NOW)
STATUS_LIST[5]=${RUN}
UPDATE_STATUS
${V_SHELL_HOME}/edw_kylin.sh
RE_CODE=$?
RUN_TIME[5]=$(DIF_TIME "${START_TIME}" "$(NOW)")
if [[ ${RE_CODE} -eq 0 ]];then
    STATUS_LIST[5]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[5]=${FAIL}
    UPDATE_STATUS
    echo "日志输出文件${V_SHELL_LOGS}/${V_DATA_DATE}/edw_kylin_${V_FREQUENCY}.log"
    exit 8
fi

## step6 综合查询入库
START_TIME=$(NOW)
STATUS_LIST[6]=${RUN}
UPDATE_STATUS
#${V_SHELL_HOME}/edw_hbase.sh
RE_CODE=$?
RUN_TIME[6]=$(DIF_TIME "${START_TIME}" "$(NOW)")
if [[ ${RE_CODE} -eq 0 ]];then
    STATUS_LIST[6]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[6]=${FAIL}
    UPDATE_STATUS
    echo "日志输出文件${V_SHELL_LOGS}/${V_DATA_DATE}/edw_hbase_${V_FREQUENCY}.log"
    exit 9
fi

## step7 抽样下载报文
START_TIME=$(NOW)
STATUS_LIST[7]=${RUN}
UPDATE_STATUS
${V_SHELL_HOME}/edw_sample.sh
RE_CODE=$?
RUN_TIME[7]=$(DIF_TIME "${START_TIME}" "$(NOW)")
if [[ ${RE_CODE} -eq 0 ]];then
    STATUS_LIST[7]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[7]=${FAIL}
    UPDATE_STATUS
    echo "日志输出文件${V_SHELL_LOGS}/${V_DATA_DATE}/edw_sample_${V_FREQUENCY}.log"
    exit 10
fi

## step8 结束任务
START_TIME=$(NOW)
STATUS_LIST[8]=${RUN}
UPDATE_STATUS
${V_SHELL_HOME}/edw_batch_end.sh
RE_CODE=$?
RUN_TIME[8]=$(DIF_TIME "${START_TIME}" "$(NOW)")
if [[ ${RE_CODE} -eq 0 ]];then
    STATUS_LIST[8]=${DONE}
    UPDATE_STATUS
else
    STATUS_LIST[8]=${FAIL}
    UPDATE_STATUS
    echo "[ERROR] 错误编码[${RE_CODE}] 日志文件[${V_SHELL_LOGS}/${V_DATA_DATE}/edw_batch_end_${V_FREQUENCY}.log]"
    exit 11
fi

printf '\n'
END
exit 0
